#include "Bike_Display_Bitmap_Definitions.h"
#include "Bike_Display_SystemSetup_Functions.h"
#include "Bike_Display_4dSystems_Functions.h"
#include "Bike_Display_HW_Profile.h"
#include "Bike_Display_ADC_Functions.h"

int nRevBarMultiplier = REVMAXLIMIT/REVBARBITMAPS;

void MainSetup(void)
{
    unsigned char uStatus = 0;
    unsigned char uGridPosition = 0;
    
    uStatus = LCDWaitTouch(1000);
    if (uStatus == 0x15)
    {
        LCDuSDImage(0, 0, SPLASHSCREEN);
        // Activate all the LEDs for about 2 secs
        PORTSetBits(IOPORT_G, OilTempLED1 | OilTempLED2 | OilTempLED3 | LightOnRelay);
        PORTSetBits(IOPORT_E, BATLowLED | NeutralLED | LeftTurnLED | RightTurnLED | \
							HighBeamLED | CheckEngineLED | OilPressureLED | LowFuelLED);
		DelayMs(1000);
        PORTClearBits(IOPORT_G, OilTempLED1 | OilTempLED2 | OilTempLED3 | LightOnRelay);
        PORTClearBits(IOPORT_E, BATLowLED | NeutralLED | LeftTurnLED | RightTurnLED | \
							HighBeamLED | CheckEngineLED | OilPressureLED | LowFuelLED);
							
        LCDCls();
        // call normal running function, tbd
    }
    else if (uStatus == 0x06)
    {
        LCDGetTouch();
        uGridPosition = LCDGridPosition();
        if (uGridPosition == 1)
        {
            //LCDGraphicString(char *s, int x, int y, char font, int color, char width, char height)
            LCDSetFont(4);
            LCDGraphicChar(0x56, 0, 0, 0x07e0, 1, 1);
        }    
        if (uGridPosition >= 1 && uGridPosition <= 6)
        {
            printf ("You pressed Button %d\n", uGridPosition);
        }
        else    
        {
            printf ("You pressed outside the grid area.\n");
        }
    }    
}

// ***** Main view functions *****

char DisplaySetup() //(char cDataToDisplay, char cGridPosition)
{
    float fVoltage = 18.8;
    char sVoltage[5] = "";
    char sHighVoltage[3] = "";
    char* sLowVoltage = sVoltage + 3;
    //static float fRevBarMultiplier;
    
    // Grid position 1
    LCDuSDImage(ICONGRIDPOSX1, ICONGRIDPOSY1, ICONGREENENGINE);
    //LCDGraphicString("888", VALUEGRIDPOSX1, VALUEGRIDPOSY1, 4, 0x07e0, 1, 1);
    LCDuSDImage(UNITGRIDPOSX1, UNITGRIDPOSY1, UNITGREENDEGCELCIUS);
    
    // Grid position 2
    LCDuSDImage(ICONGRIDPOSX2, ICONGRIDPOSY2, ICONGREENOILCAN);
    //LCDGraphicString("888", 48, 40, 4, 0x07e0, 1, 1);
    LCDuSDImage(UNITGRIDPOSX2, UNITGRIDPOSY2, UNITGREENDEGCELCIUS);
    
    // Grid position 3
    LCDuSDImage(ICONGRIDPOSX3, ICONGRIDPOSY3, ICONGREENBATTERIE);
    sprintf(sVoltage, "%0.1f", fVoltage);
    strncpy(sHighVoltage, sVoltage, 2);
    LCDGraphicString(sHighVoltage, VALUEGRIDPOSX3 - 8, VALUEGRIDPOSY3, 4, 0x07e0, 1, 1);
    LCDuSDImage(VALUEGRIDPOSX3 - 8 + (2 * 21), VALUEGRIDPOSY3, DPSMALLGREEN);
    LCDGraphicString(sLowVoltage, 90, VALUEGRIDPOSY3, 4, 0x07e0, 1, 1);
    LCDuSDImage(UNITGRIDPOSX3, UNITGRIDPOSY3, UNITGREENVOLTAGE);
    
    // Display mph
    //LCDGraphicString("188", 147, 30, 4, 0x07e0, 2, 2);
    LCDuSDImage(270, 53, UNITGREENMPH);

    // Display rpm
    //LCDGraphicString("8850", 25, 135, 4, 0x07e0, 3, 3);
    LCDuSDImage(270, 183, UNITGREENRPM);
        
    //LCDuSDImage(140, 5, AirIntake1);
    //LCDuSDImage(140, 40, AirIntake2);
    //LCDuSDImage(140, 75, AirIntake3);
}        

char DisplayEngineTemp(int nNewEngineTemp)
{
    static int nCurrentEngineColour = 0;
    static int nCurrentEngineTemp = 999;
    int nNewColour = 0;
    bool bColourChanged = false;
    bool bNegativeChanged = false;
    
    if(nNewEngineTemp != nCurrentEngineTemp)
    {
        if((nCurrentEngineTemp < -9) && (nNewEngineTemp > -10)) bNegativeChanged = true;
        if((nCurrentEngineTemp < 0) && (nNewEngineTemp >= 0)) bNegativeChanged = true;
        if(nNewEngineTemp <= ENGINECOLD)
        {
            nNewColour = BLUETEXT;
        }
        else if(nNewEngineTemp > ENGINEHOT)        
        {
            nNewColour = REDTEXT;
        }
        else       
        {
            nNewColour = GREENTEXT;
        }
        if(nNewColour != nCurrentEngineColour)
        {
            bColourChanged = true;
            if(nNewColour == REDTEXT)
            {
                LCDuSDImage(ICONGRIDPOSX1, ICONGRIDPOSY1, ICONREDENGINE);
                LCDuSDImage(UNITGRIDPOSX1, UNITGRIDPOSY1, UNITREDDEGCELCIUS);
            }
            else if(nNewColour == GREENTEXT)        
            {
                LCDuSDImage(ICONGRIDPOSX1, ICONGRIDPOSY1, ICONGREENENGINE);
                LCDuSDImage(UNITGRIDPOSX1, UNITGRIDPOSY1, UNITGREENDEGCELCIUS);
            }
            else       
            {
                LCDuSDImage(ICONGRIDPOSX1, ICONGRIDPOSY1, ICONBLUEENGINE);
                LCDuSDImage(UNITGRIDPOSX1, UNITGRIDPOSY1, UNITBLUEDEGCELCIUS);
            }
            nCurrentEngineColour = nNewColour;
        }    
        MyLCDDisplayValue(nNewEngineTemp, nCurrentEngineTemp, bColourChanged, bNegativeChanged, nNewColour, TOPLEFT1);
        nCurrentEngineTemp = nNewEngineTemp;
    }   
}

char DisplayOilTemp(int nNewOilTemp)
{
    static int nCurrentOilColour = 0;
    static int nCurrentOilTemp = 999;
    int nNewColour = 0;
    bool bColourChanged = false;
    bool bNegativeChanged = false;
        
    if (nNewOilTemp != nCurrentOilTemp)
    {
        if((nCurrentOilTemp < -9) && (nNewOilTemp > -10)) bNegativeChanged = true;
        if((nCurrentOilTemp < 0) && (nNewOilTemp >= 0)) bNegativeChanged = true;
        if (nNewOilTemp <= OILCOLD)
        {
            nNewColour = BLUETEXT;
        }
        else if (nNewOilTemp > OILHOT)        
        {
            nNewColour = REDTEXT;
        }
        else       
        {
            nNewColour = GREENTEXT;
        }
        if (nNewColour != nCurrentOilColour)
        {
            bColourChanged = true;
            if (nNewColour == REDTEXT)
            {
                LCDuSDImage(ICONGRIDPOSX2, ICONGRIDPOSY2, ICONREDOILCAN);
                LCDuSDImage(UNITGRIDPOSX2, UNITGRIDPOSY2, UNITREDDEGCELCIUS);
            }
            else if (nNewColour == GREENTEXT)        
            {
                LCDuSDImage(ICONGRIDPOSX2, ICONGRIDPOSY2, ICONGREENOILCAN);
                LCDuSDImage(UNITGRIDPOSX2, UNITGRIDPOSY2, UNITGREENDEGCELCIUS);
            }
            else       
            {
                LCDuSDImage(ICONGRIDPOSX2, ICONGRIDPOSY2, ICONBLUEOILCAN);
                LCDuSDImage(UNITGRIDPOSX2, UNITGRIDPOSY2, UNITBLUEDEGCELCIUS);
            }
            nCurrentOilColour = nNewColour;
        }    
        MyLCDDisplayValue(nNewOilTemp, nCurrentOilTemp, bColourChanged, bNegativeChanged, nNewColour, TOPLEFT2);
        nCurrentOilTemp = nNewOilTemp;
    }
}    

char DisplayTPSPercent(int nNewTPSPercent)
{
    static int nCurrentTPSColour = 4;
    static int nCurrentTPSPercent = 199;
    int nNewColour = 0;
    bool bColourChanged = false;
    bool bNegativeChanged = false;
    
    if (nNewTPSPercent != nCurrentTPSPercent)
    {
        if (nNewTPSPercent <= TPSPERCENTMIN)
        {
            nNewColour = REDTEXT;
        }
        else       
        {
            nNewColour = GREENTEXT;
        }
        if (nNewColour != nCurrentTPSColour)
        {
            bColourChanged = true;
            if (nNewColour == REDTEXT)
            {
                LCDuSDImage(ICONGRIDPOSX3, ICONGRIDPOSY3, ICONREDTPS);
                LCDuSDImage(UNITGRIDPOSX3, UNITGRIDPOSY3, UNITREDPERCENT);
            }
            else       
            {
                LCDuSDImage(ICONGRIDPOSX3, ICONGRIDPOSY3, ICONGREENTPS);
                LCDuSDImage(UNITGRIDPOSX3, UNITGRIDPOSY3, UNITGREENPERCENT);
            }
            nCurrentTPSColour = nNewColour;
        }    
        MyLCDDisplayValue(nNewTPSPercent, nCurrentTPSPercent, bColourChanged, bNegativeChanged, nNewColour, TOPLEFT3);
        nCurrentTPSPercent = nNewTPSPercent;
    }
}    

char DisplaySpeed(int nNewSpeed)
{
    int nNewColour = 0;
    static int nCurrentSpeed = 999;
    static int nCurrentSpeedColour = 0;
    bool bColourChanged = false;
    bool bNegativeChanged = false;
    
    if (nNewSpeed != nCurrentSpeed)
    {
        if(nNewSpeed > SPEEDHIGH)        
        {
            nNewColour = REDTEXT;
        }
        else       
        {
            nNewColour = GREENTEXT;
        }
        if (nNewColour != nCurrentSpeedColour)
        {
            bColourChanged = true;
            if(nNewColour == REDTEXT)
            {
                LCDuSDImage(UNITMEDIUMX, UNITMEDIUMY, UNITREDMPH);
            }
            else      
            {
                LCDuSDImage(UNITMEDIUMX, UNITMEDIUMY, UNITGREENMPH);
            }
            nCurrentSpeedColour = nNewColour;
        }    
        MyLCDDisplayValue(nNewSpeed, nCurrentSpeed, bColourChanged, bNegativeChanged, nNewColour, TOPRIGHT);
        nCurrentSpeed = nNewSpeed;
    }
}    

char DisplayRevs(int nNewRevs)
{
    static int nCurrentRevColour = 0;
    static int nCurrentRevs = 19999;
    int nNewColour = 0;
    int nBarColour = 0;
    bool bColourChanged = false;
    bool bNegativeChanged = false;
    
    if (nNewRevs != nCurrentRevs)
    {
        if (nNewRevs <= REVLOW)
        {
            nNewColour = BLUETEXT;
            nBarColour = BLUE;
        }
        else if (nNewRevs > REVHIGH)        
        {
            nNewColour = REDTEXT;
            nBarColour = RED;
        }
        else       
        {
            nNewColour = GREENTEXT;
            nBarColour = GREEN;
        }        
        if (nNewColour != nCurrentRevColour)
        {
            bColourChanged = true;
            if (nNewColour == REDTEXT)
            {
                LCDuSDImage(UNITBOTTOMX, UNITBOTTOMY, UNITREDRPM);
            }
            else if (nNewColour == GREENTEXT)        
            {
                LCDuSDImage(UNITBOTTOMX, UNITBOTTOMY, UNITGREENRPM);
            }
            else       
            {
                LCDuSDImage(UNITBOTTOMX, UNITBOTTOMY, UNITBLUERPM);
            }
            nCurrentRevColour = nNewColour;
        }    
        MyLCDDisplayValue(nNewRevs, nCurrentRevs, bColourChanged, bNegativeChanged, nNewColour, BOTTOM);
        nCurrentRevs = nNewRevs;
        DisplayRevsBar(nCurrentRevs);
    }
}    

char DisplayRevsBar(int nNewRevs)
{
    int nRevBarAddr = 0;
    int nNewRevBarAmount = 0;
    static int nCurrentRevBarAmount = 0;

    nNewRevBarAmount = nNewRevs/nRevBarMultiplier;
    
    if(nCurrentRevBarAmount != nNewRevBarAmount)
    {
        nRevBarAddr = REVBARSTART + nNewRevBarAmount * REVBARADDRINC;
        LCDuSDImage(0, 210, nRevBarAddr);
        nCurrentRevBarAmount = nNewRevBarAmount;
    }    
}

void ScanDigitalSignalsFromBike(void)
{
    //Check the input and write to output as true or false statement using single line if else statement 
    PORTReadBits(IOPORT_D, NeutralIn) ? PORTClearBits(IOPORT_E, NeutralLED) : PORTSetBits(IOPORT_E, NeutralLED);
    PORTReadBits(IOPORT_D, CheckEngineIn) ? PORTClearBits(IOPORT_E, CheckEngineLED) : PORTSetBits(IOPORT_E, CheckEngineLED);
    PORTReadBits(IOPORT_D, OilPressureIn) ? PORTClearBits(IOPORT_E, OilPressureLED) : PORTSetBits(IOPORT_E, OilPressureLED);
    PORTReadBits(IOPORT_D, LowFuelIn) ? PORTClearBits(IOPORT_E, LowFuelLED) : PORTSetBits(IOPORT_E, LowFuelLED);

    PORTReadBits(IOPORT_D, LeftTurnIn) ? PORTSetBits(IOPORT_E, LeftTurnLED) : PORTClearBits(IOPORT_E, LeftTurnLED);
    PORTReadBits(IOPORT_D, RightTurnIn) ? PORTSetBits(IOPORT_E, RightTurnLED) : PORTClearBits(IOPORT_E, RightTurnLED);
    PORTReadBits(IOPORT_D, HighBeamIn) ? PORTSetBits(IOPORT_E, HighBeamLED) : PORTClearBits(IOPORT_E, HighBeamLED);
}    

void MeasureBatteryVoltage(void)
{
    int nBatteryVoltage_mV;// = 0;
    int nBatteryVoltage_V = 0;
    int nRemainder = 0;
    
    // calculation needs to be done in mV so no floating point variable is required
    // to help precission multiply MAXVOLTAGE by 1000 and devide the result again by same amount
    nBatteryVoltage_mV = (ReadAnalogInput(BATTERY) * (MAXVOLTAGE * 1000 / 1023)) / 1000;
    
    nRemainder = nBatteryVoltage_mV % 1000;
    
    nBatteryVoltage_V = nBatteryVoltage_mV / 1000;
    
    //DBPRINTF("Analog Input 0 = %d.%dV\n\n", nBatteryVoltage_V, nRemainder);
    
    if(nBatteryVoltage_mV < MINVOLTAGE)
    {
        PORTSetBits(IOPORT_E, BATLowLED);
    }
    else
    {
        PORTClearBits(IOPORT_E, BATLowLED);
    }        
}

void MeasureOilTemperature(void)
{
    INT64 nOilTempCalcHelper = 0;
    int nOilTemp_mV = 0;
    int nOilTemp_degC = 0;
    int nRemainder = 0;
    
    // calculation needs to be done in mV so no floating point variable is required
    // to help precission multiply MAXVOLTAGE by 1000 and devide the result again by same amount
    nOilTempCalcHelper = 5000 * ReadAnalogInput(OILTEMP);
    
    nOilTempCalcHelper = nOilTempCalcHelper * 3300 * 1000 / 1023;
    
    nOilTemp_mV = ((INT64)nOilTempCalcHelper / 1000) / 3300;
    
    nOilTemp_degC = ((nOilTemp_mV - 250)/22.5)-50;
    
    DisplayOilTemp(nOilTemp_degC);
    //DBPRINTF("Analog Input 1 = %ddegC\n\n", nOilTemp_degC);
}
    
    
    